package MenuUnit
{
	import Assist.JPGEncoder;
	import Assist.PNGEncoder;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import Map.MapManager;
	
	import fl.controls.Button;
	import fl.controls.TextInput;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import mx.core.ButtonAsset;

	public class MapEditMenu extends Menu
	{
		public var MapImg:Bitmap;
		public var MapSourceSprite:Sprite;
		public var Ready:Boolean = false;
		public var ReadyTile:Point = new Point( -1, -1);
		public var StartPos:Point = new Point(0, 0);
		public var dragMapMake:Boolean = false;
		
		public var MapMaker:Sprite = new Sprite();
		public var MapMakerWidth:int = 1500;
		public var MapMakerHeight:int = 1500;
		public var MapStart:Boolean = false;		
		
		public var bmpArray:ByteArray ;
		public var Thumbnail:BitmapData;
		
		public var SelectGrain:Boolean = false;
		
		public var loader:Loader = new Loader();
		public var i:int = 0;
		
		public var MapSource:BitmapData;
		public var TilesNum:int;
		public var MapWidthNum:int = 23;
		
		public function MapEditMenu()
		{
			
		}
		
		public override function Active():void
		{
			super.Active();
						
			MapImg = new Bitmap(MapManager.MapSource);
			MapSourceSprite = new Sprite();
			
			MapSourceSprite.addChild(MapImg);
			Add(MapSourceSprite, 0, 0);
			Ready = false;
			
			var space:Sprite = new Sprite();
			Add(space);
			space.graphics.beginFill(0x0000ff);
			space.graphics.drawRect(0, 0, MenuManager.GameWidth, 25);
			space.y = 310;
			
			var space1:Sprite = new Sprite();
			Add(space1);
			space1.graphics.beginFill(0x22ddff);
			space1.graphics.drawRect(0, 0, 70, 25);
			space1.y = 310;
			space1.x = 90;
			
			var space2:Sprite = new Sprite();
			Add(space2);
			space2.graphics.beginFill(0x6C2E2E);
			space2.graphics.drawRect(0, 0, 70, 25);
			space2.y = 310;
			space2.x = 200
			
			var space3:Sprite = new Sprite();
			Add(space3);
			space3.graphics.beginFill(0x666666);
			space3.graphics.drawRect(0, 0, 80, 25);
			space3.y = 310;
			space3.x = 500
			
			var space4:Sprite = new Sprite();
			Add(space4);
			space4.graphics.beginFill(0x666666);
			space4.graphics.drawRect(0, 0, 80, 25);
			space4.y = 310;
			space4.x = 600

			this.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void
			{
				if (!HitRect(new Point(e.localX, e.localY), new Rectangle(MapSourceSprite.x, MapSourceSprite.y, MapSourceSprite.width, MapSourceSprite.height)))
				{
					if (ReadyTile != new Point( -1, -1))
					{
						MapStart = true;
					}
				}
			});
			
			MapSourceSprite.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void
			{
				ReadyTile = GetTiles(e.localX, e.localY);		
				if (SelectGrain)
				{
					SelectGrain = false;
					MapManager.MapGrain = ReadyTile;
				}
			});			
			
			Add(MapMaker);	
			MapMaker.x = 0;
			MapMaker.y = 350;
			MapMaker.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);			
			
			var mask:Sprite = new Sprite();
			mask.graphics.beginFill(0xff0000);
			mask.graphics.drawRect(0, 342, MenuManager.GameWidth, 390);
			Add(mask);
			MapMaker.mask = mask;
			
			var txt:TextField = new TextField();
			txt.text = "拖拽地图";
			Add(txt);
			txt.x = 100;
			txt.y = 310;
			txt.selectable = false;
			txt.textColor = 0x000000;
			txt.height = space.height;
			txt.borderColor = 0xffcaac;
			txt.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void { dragMapMake = !dragMapMake; } );
			
			var txt2:TextField = new TextField();
			txt2.text = "保存地图";
			Add(txt2);
			txt2.x = 210;
			txt2.y = 310;
			txt2.selectable = false;
			txt2.textColor = 0xffffff;
			txt2.height = space.height;
			txt2.borderColor = 0xffcaac;
			txt2.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void { SaveImg(); } );
			
			var inputW:TextInput = new TextInput();
			inputW.text = "地图宽度";
			Add(inputW);
			inputW.x = 290;
			inputW.y = 315;
			var inputH:TextInput = new TextInput();
			inputH.text = "地图高度";
			Add(inputH);
			inputH.x = 390;
			inputH.y = 315;
			
			var txt3:TextField = new TextField();
			txt3.text = "确定大小";
			Add(txt3);
			txt3.x = 510;
			txt3.y = 310;
			txt3.selectable = false;
			txt3.textColor = 0x000000;
			txt3.height = space.height;
			txt3.borderColor = 0xffcaac;
			txt3.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
			{ 
				var w:int = parseInt(inputW.text);
				var h:int = parseInt(inputH.text);
				ChangeSize(w,h); 
			} );
			
			var txt4:TextField = new TextField();
			txt4.text = "返回";
			Add(txt4);
			txt4.x = 620;
			txt4.y = 310;
			txt4.selectable = false;
			txt4.textColor = 0xf;
			txt4.height = space.height;
			txt4.borderColor = 0xffcaac;
			txt4.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
			{ 
				MenuManager.SwitchMenu("LoginMenu");
			} );
			
			var txt5:TextField = new TextField();
			txt5.text = "选择底纹";
			Add(txt5);
			txt5.x = 690;
			txt5.y = 310;
			txt5.selectable = false;
			txt5.textColor = 0xf;
			txt5.height = space.height;
			txt5.borderColor = 0xffcaac;
			txt5.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
			{ 
				SelectGrain = true;
				MakeSource(127);				
			} );
			
			
			MapMaker.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void {
				if (dragMapMake)
				{
					MapMaker.startDrag();
				}
				else
				{
					var x:int = e.localX / MapManager.tileWidth;
					var y:int = e.localY / MapManager.tileHeight;
					AddOne(ReadyTile.x, ReadyTile.y, x * MapManager.tileWidth, y * MapManager.tileHeight);						
				}
			});
			MapMaker.addEventListener(MouseEvent.MOUSE_UP,function(e:MouseEvent):void {
				if (dragMapMake)
				{
					MapMaker.stopDrag();
				}
				else
				{
					
				}
			});			
		}
		
		public function GetTiles(_x:int, _y:int):Point
		{			
			var xp:int = _x / MapManager.tileWidth;
			var yp:int = _y / MapManager.tileHeight;
			return(new Point(xp, yp));
		}
		
		public static function HitRect(_p1:Point, _rec:Rectangle):Boolean
		{
			if ((_p1.x < _rec.x) || (_p1.x > _rec.x + _rec.width))
			   return false;
			if ((_p1.y < _rec.y) || (_p1.y > _rec.y + _rec.height))
			   return false;
			 		
			return true;
		}
		
		public  function ChangeSize(_w:int,_h:int):void
		{
			MapMakerWidth = _w;
			MapMakerHeight = _h;
			var w:int = MapMakerWidth / MapManager.tileWidth;
			var h:int = MapMakerHeight / MapManager.tileHeight;
			Thumbnail = new BitmapData(w, h);  
			MapMaker.graphics.beginFill(0x00ccad);
			MapMaker.graphics.drawRect(0, 0, MapMakerWidth, MapMakerHeight);
		}
		
		public function AddOne(_x:int,_y:int,_xDraw:int,_yDraw:int):void
		{
			var dir:Dictionary = MapManager.Tiles;
			var pos:String = _x.toString() + "-" + _y.toString();
			var bmpData:BitmapData = MapManager.Tiles[pos];
			var bmp:Bitmap = new Bitmap();
			bmp.bitmapData = bmpData;
			MapMaker.addChild(bmp);
			bmp.x = _xDraw;
			bmp.y = _yDraw;
			
			var pixel:int =  MapManager.clrArray[pos];
			Thumbnail.setPixel(_xDraw/MapManager.tileWidth, _yDraw/MapManager.tileHeight, pixel);		
			trace(_xDraw, _yDraw);
		}		
		
		
		public function SaveImg():void
		{
			var thumbnail:Bitmap = new Bitmap(Thumbnail);
			addChild(thumbnail);
			thumbnail.x = 1000;
			thumbnail.y = 340;
			
			var png:PNGEncoder = new PNGEncoder();
			var bry:ByteArray = PNGEncoder.encode(Thumbnail);
			var file:FileReference = new FileReference();
			file.save(bry, "ThumbnailPng.png");
		}
		
		public function MakeSource(_tilesNum:int):void
		{
			TilesNum = _tilesNum;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnMakeSource); 
			LoadStep();
			var w:int = MapWidthNum;			
			var h:int = TilesNum / w;
			if(TilesNum % w != 0)
			   h++;
			MapSource = new BitmapData(w * MapManager.tileWidth, h * MapManager.tileHeight);
			LoadStep();
		}
		
		public function LoadStep():void
		{
			i++;
			if (i > TilesNum)
			{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, OnMakeSource);
				var png:PNGEncoder = new PNGEncoder();
			    var bry:ByteArray = PNGEncoder.encode(MapSource);
			    var file:FileReference = new FileReference();
			    file.save(bry, "MapSourceMake.png");
				return;
			}
			var url:String = "../Resource/Image/Tiles/" + i.toString() + ".png";
			loader.load(new URLRequest(url));			
		}
		
		public function OnMakeSource(e:Event):void
		{
			var loader2:Loader = (e.target.loader) as Loader;			
			var bmpData:BitmapData = (loader2.content as Bitmap).bitmapData;
			var x:int = (i % MapWidthNum) * MapManager.tileWidth;
			var y:int = ((int)(i / MapWidthNum)) * MapManager.tileHeight;
			MapSource.copyPixels(bmpData, new Rectangle(0, 0, bmpData.width, bmpData.height), new Point(x, y));
			LoadStep();
		}
		
		public function OnKeyDown(e:KeyboardEvent):void
		{
			trace(e.keyCode, ":", e.keyLocation);
		}
	}
}